package com.example.demo_redis_test.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {
    private static final Logger log = LoggerFactory.getLogger(RedisController.class);

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping("/getA.do")
    public String getA(){
        return stringRedisTemplate.opsForValue().get("a");
    }

    @RequestMapping("/getRedisDate.do")
    public String getRedisDate(){
        String  sssss= stringRedisTemplate.opsForValue().get("testRedis");//根据key获取缓存中的val
        //存入永久数据
        if("".equals(sssss)|| sssss == null){
            stringRedisTemplate.opsForValue().set("testRedis","100");
            String  sssssss= findName("testRedis");
            return "testRedis新设置的值："+sssssss;
        }
        return "testRedis原有的值："+sssss;
    }

    @RequestMapping("/getRedis.do")
    public String getRedis(){
        stringRedisTemplate.opsForValue().set("baike", "100", 60 * 10, TimeUnit.SECONDS);
        return findName("baike");
        //根据key获取过期时间并换算成指定单位
        //long n5 = stringRedisTemplate.getExpire("baike",TimeUnit.SECONDS);
    }
    @Scheduled(cron = "0 * 18 * * ? ")
    public String getScheduled(){
        String UUID_String = "111111111";
        Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(UUID_String, "lock",
                11000L, TimeUnit.MILLISECONDS);
        System.out.println(success);
        if (success){
            System.out.println("获取锁成功！等待11秒");
            stringRedisTemplate.delete(UUID_String);
            System.out.println("释放锁！");
        }
        return "Success!";
    }
    /**
     * redis抢红包(简单测试版)
     * @param ii
     * @param kk
     */
    @RequestMapping("/SplitRedPacket.do")
    public void SplitRedPacket(int ii,int kk){
        int[] packets= SplitRedPacket11(ii,kk);
        for (int packet : packets) {
            stringRedisTemplate.opsForList().rightPush("RedPacket",String.valueOf(packet));
        }
        List<String> listAll111 = stringRedisTemplate.opsForList().range( "RedPacket", 0, -1);
        for(int i = 0;i<packets.length;i++){
            String getRedPacket = stringRedisTemplate.opsForList().rightPop("RedPacket");
            System.out.println(getRedPacket);
        }
        List<String> listAll222 = stringRedisTemplate.opsForList().range( "RedPacket", 0, -1);
    }

    /**
     * @Description 获取String类型的value
     * @param name
     * @return
     */
    public String findName(String name) {
        if (name==null){
            log.error("===============key为null======================================================");
        }
        return stringRedisTemplate.opsForValue().get(name);
    }

    /**
     * 拆红包，1、红包金额呀被全部拆分完毕，2、红包金额不能差的太离谱
     */
    public static int[] SplitRedPacket11(int total,int count){
        int use = 0;
        int[] arry = new int[count];
        Random random = new Random();
        for (int i =0;i < count;i++){
            if (i== count - 1)
                arry[i] = total - use;
            else {
                int avg = (total -use) *2 /(count -i); //2 红包随机金额浮动系数
                arry[i] = 1+random.nextInt(avg -1);
            }
            use = use +arry[i];
        }
        return arry;
    }

    /**
     * 测试：https://www.cnblogs.com/zxporz/p/10799335.html
     *
     */
}
